//
//  MusicHeader.m
//  A_program
//
//  Created by 杨利佳 on 15/5/23.
//  Copyright (c) 2015年 BayMax. All rights reserved.
//

#import "MusicHeader.h"
#import "MusicThiButton.h"
#import "LoginViewController.h"
#import "NovelDatabase.h"
#import "NovelModel.h"

@implementation MusicHeader

- (void)dealloc
{
    [_detail release];
    [_name release];
    [_image1 release];
    [_showImage release];
    [_title release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.background = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        _background.alpha = 0.1;
        [self addSubview:_background];
        [_background release];
        [self createView];
    }
    return self;
}

- (void)createView
{
        
    // 标题
    self.title = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, self.frame.size.width, 30)];
    _title.textAlignment = NSTextAlignmentCenter;
    _title.font = [UIFont boldSystemFontOfSize:25];
    [self addSubview:_title];
    [_title release];
    
    // 专辑图片
    self.showImage = [[UIImageView alloc] initWithFrame:CGRectMake(20, 70, 100, 100)];
    _showImage.backgroundColor = [UIColor whiteColor];
    [self addSubview:_showImage];
    [_showImage release];
    
    // 小图标
    self.image1 = [[UIImageView alloc] initWithFrame:CGRectMake(130, 70, 20, 20)];
    [self addSubview:_image1];
    [_image1 release];
    
    //
    self.name = [[UILabel alloc] initWithFrame:CGRectMake(160, 70, self.frame.size.width - 160, 20)];
    [self addSubview:_name];
    [_name release];
    
    // 简介, 没加载除数据之前为"暂无简介"
    self.detail = [[UILabel alloc] initWithFrame:CGRectMake(130, 120, 200, 30)];
    _detail.text = @"暂无简介";
    _detail.textColor = [UIColor whiteColor];
    _detail.font = [UIFont boldSystemFontOfSize:13];
    [self addSubview:_detail];
    [_detail release];
    
    // 收藏按钮
    self.coll = [[MusicThiButton alloc] initWithFrame:CGRectMake(self.frame.size.width / 3 * 2, 180, self.frame.size.width / 3, 30)];
    self.coll.layer.borderWidth = 1;
    self.coll.layer.borderColor = [UIColor whiteColor].CGColor;
    UIImage *aImage1 = [UIImage imageNamed:@"star.png"];
    UIImage *bImage1 = [aImage1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self.coll setImage:bImage1 forState:UIControlStateNormal];

    self.coll.titleLabel.font = [UIFont boldSystemFontOfSize:13];
    [self.coll addTarget:self action:@selector(collAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.coll];
    [self.coll release];
    
}


// 收藏按钮
- (void)collAction:(id)sender
{
#pragma mark-
#pragma mark 判断是否已收藏
    NSUserDefaults *myLoginName = [NSUserDefaults standardUserDefaults];
    NSInteger myAlbumId = self.coll.albumId;
    NSMutableArray *array = (NSMutableArray *)[NovelDatabase selectNameAlbumId:myAlbumId loginName:[myLoginName objectForKey:@"loginName"]];
    if ([array count] == 0 && [self.coll.titleLabel.text isEqualToString:@"收藏"]) {
        
        NSString *collect = [NSString stringWithFormat:@"%ld",self.coll.albumId];
        [self.musicThiButtonActionDelegate createCollectAction:collect];
        
        if (![[myLoginName objectForKey:@"loginName"] length] == 0 &&[[myLoginName objectForKey:@"state"]isEqualToString:@"登录"]) {
            [self.coll setTitle:@"已收藏" forState:UIControlStateNormal];
        }else{
            NSLog(@"未登录或则没有用户");
            
            
        }
        
    }else if([self.coll.titleLabel.text isEqualToString:@"已收藏"]){
        
        NovelModel *myid = [[NovelModel alloc]init];
        for (NovelModel *model in array) {
            myid = model;
        }
        [NovelDatabase deleteWithModel:myid];
        [self.coll setTitle:@"收藏" forState:UIControlStateNormal];
        
    }else{
        NSMutableArray *array = (NSMutableArray *)[NovelDatabase selectNameAlbumId:self.coll.albumId loginName:[myLoginName objectForKey:@"loginName"]];
        if ( ![array count] == 0) {
            [self.coll setTitle:@"已收藏" forState:UIControlStateNormal];
        }
    }

}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
